const app = getApp();
Page({
  onShareAppMessage: function () {
      return {
        title: 'AEZOCN',
        desc: 'AEZOCN'
      }
  },
  data: {
    userInfo: {},
    imgbx:{
      topImg:'/images/p1-top.png',
      titImg:'/images/p1-tit.png',
      bomImg:'/images/p1-bom.png'
    },
    button: {
      defaultSize: 'default',
      disabled: false,
      plain: false,
      loading: false
    },
    inputContent: {
      minRent: 1000,
      maxRent: 3000,
      email: ''
    },
    region: [1, 70, 186],
    multiIndex: [0, 0, 0],
    multiArray: [[{ id: 1, name: "上海" }], [{ id: 70, name: "杨浦" }], [{ id: 186, name: "江浦路" }]],
    rentResources: [{
      name: '安居客',
      value: 'anjuke',
      checked: true
    }, {
      name: '58同城',
      value: '58',
      checked: false
    }],
    resources: ['anjuke'],
  },
  onLoad () {
    let that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo: userInfo
      });
    })

    // 获取初始地点
    this.findRegion(0, 0);
    this.findRegion(this.data.multiArray[0][0].id, 1);
    this.findRegion(this.data.multiArray[1][0].id, 2);
  }, 
  findHouse (e){
    let _this  = this;
    let _userInfo = _this.data.userInfo;
    let _minRent = _this.data.inputContent.minRent;
    let _maxRent = _this.data.inputContent.maxRent;
    let _region = _this.data.region;
    let _resources = _this.data.resources;
    let _email = _this.data.inputContent.email;

    console.log(_userInfo, _minRent, _maxRent, _region, _resources);

    if (_userInfo.openid == null || _userInfo.openid == "") {
      wx.showModal({
        title: '提示',
        content: '请授权小程序获取您的用户信息',
        showCancel: false
      });

      //调用应用实例的方法获取全局数据
      app.getUserInfo(function (userInfo) {
        //更新数据
        that.setData({
          userInfo: userInfo
        });
      })
      return false;
    }

    if (typeof (_minRent) == 'undefined' || !_minRent
      || typeof (_maxRent) == 'undefined' || !_maxRent){
       wx.showModal({
          title: '提示',
          content: '请输入租金范围',
          showCancel:false
       });
       return false;
    }

    if (_this.data.multiArray[1][_this.data.multiIndex[1]].name == "全部") {
      wx.showModal({
        title: '提示',
        content: '地点请具体到区域',
        showCancel: false
      });
      return false;
    }

    if (_resources.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择房源',
        showCancel: false
      });
      return false;
    }

    if (_email == "") {
      wx.showModal({
        title: '提示',
        content: '请输入邮箱用于接收最新房源信息',
        showCancel: false
      });
      return false;
    } else {
      var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/; 
      if (!reg.test(_email)) {
        wx.showModal({
          title: '提示',
          content: '请输入正确的邮箱地址',
          showCancel: false
        });
        return false;
      }
    }

    // wx.navigateTo({
    //   url: '../content/content?type='+_datas
    // });

    let data = {
      openid: _userInfo.openid,
      nick_name: _userInfo.nickName,
      minRent: _minRent,
      maxRent: _maxRent,
      region: _region,
      resources: _resources,
      email: _email,
      _source: "wx_renthouse"
    };
    // console.log(JSON.stringify(data));
    wx.request({
      method: "POST",
      url: 'https://api.aezo.cn/api/weixin/renthouse/subscibe.php',
      // url: 'http://localhost/aezo/api/weixin/renthouse/subscibe.php',
      data: data,
      success: (res) => {
        console.log(res)
        if (res.data.status == 200) {
          wx.showModal({
            title: '提示',
            content: '订阅成功',
            showCancel: false
          });
        } else {
          wx.showModal({
            title: '错误',
            content: res.data.message != null ? res.data.message : '订阅失败',
            showCancel: false
          });
        }
      }
    })
  },
  cancelHouse: function(e) {
    let that = this;
    let data = {
      openid: that.data.userInfo.openid,
      _source: "wx_renthouse"
    };

    if (data.openid == null || data.openid == "") {
      wx.showModal({
        title: '提示',
        content: '请授权小程序获取您的用户信息',
        showCancel: false
      });

      //调用应用实例的方法获取全局数据
      app.getUserInfo(function (userInfo) {
        //更新数据
        that.setData({
          userInfo: userInfo
        });
      })
      return false;
    }
    
    wx.request({
      method: "POST",
      url: 'https://api.aezo.cn/api/weixin/renthouse/subscibe.php?type=cancel',
      // url: 'http://localhost/aezo/api/weixin/renthouse/subscibe.php?type=cancel',
      data: data,
      success: (res) => {
        console.log(res)
        if (res.data.status == 200) {
          wx.showModal({
            title: '提示',
            content: '取消成功',
            showCancel: false
          });
        } else {
          wx.showModal({
            title: '错误',
            content: res.data.message != null ? res.data.message : '取消失败',
            showCancel: false
          });
        }
      }
    })
  },
  // 输入框发生改变：赋值到inputContent
  bindchangeInput (e) {
    this.data.inputContent[e.currentTarget.id] = e.detail.value;
  },
  checkboxChange: function (e) {
    let resources = e.detail.value;
    this.setData({
      resources: resources
    });
  
    for (var i in resources) {
      if (resources[i] != 'anjuke') {
        wx.showModal({
          title: '提示',
          content: '目前只支持安居客',
          showCancel: false
        });
        return false;
      }
    }
  },
  // 点击地点
  regionTap: function(e) {
    
  },
  // 地点的列改变
  bindMultiPickerChange: function (e) {
    let multiIndex = this.data.multiIndex;
    multiIndex[e.detail.column] = e.detail.value;
    this.setData({
      multiIndex: multiIndex
    });

    switch (e.detail.column){
      case 0:
        this.findRegion(this.data.multiArray[0][e.detail.value].id, 1);
        break;
      case 1:
        this.findRegion(this.data.multiArray[1][e.detail.value].id, 2);
        break;
    }
  },
  // 地点点击确定
  bindMultiChange: function(e) {
    let region = this.data.region;
    region[0] = this.data.multiArray[0][e.detail.value[0]].id
    region[1] = this.data.multiArray[1][e.detail.value[1]].id
    region[2] = this.data.multiArray[2][e.detail.value[2]].id

    this.setData({
      region: region
    });

    console.log(this.data.region);
  },
  findRegion: function (pid, index) {
    let that = this;
    wx.request({
      method: "GET",
      url: 'https://api.aezo.cn/api/weixin/renthouse/region.php?pid=' + pid,
      // url: 'http://localhost/aezo/api/weixin/renthouse/region.php?pid=' + pid,
      success: (res) => {
        let data = {
          multiArray: that.data.multiArray
        }

        if (res != null && res.data != null && res.data[0] != null) {
          data.multiArray[index] = res.data;
        }

        that.setData(data);
      }
    })
  }
})
